package cz.muni.fi.pa165.ws.converter;

import cz.muni.fi.pa165.sme.api.to.BurnedCaloriesTO;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.BurnedCalories;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author forman
 */
public class BurnedCaloriesConverter {

	@Autowired
	private SportActivityConverter converter;

	public BurnedCalories convert2BurnedCalories(final BurnedCaloriesTO burnedCaloriesTO) {

		if (burnedCaloriesTO == null) {
			throw new IllegalArgumentException("Cannot convert empty entity");
		}
		final BurnedCalories burnedCalories = new BurnedCalories();
		burnedCalories.setId(burnedCaloriesTO.getId().toString());
		burnedCalories.setCalories(burnedCaloriesTO.getCalories().toString());
		burnedCalories.setSpeed(burnedCaloriesTO.getSpeed().toString());
		burnedCalories.setWeight(burnedCaloriesTO.getWeight().toString());
		if (burnedCaloriesTO.getSportActivity() == null) {
			throw new IllegalArgumentException("Burned calories cannot have empty sportActivity");
		}
		burnedCalories.setSportActivity(converter.convert2SportActivity(burnedCaloriesTO.getSportActivity()));
		return burnedCalories;
	}

	public BurnedCaloriesTO convert2BurnedCaloriesTO(final BurnedCalories burnedCalories) {

		if (burnedCalories == null) {
			throw new IllegalArgumentException("Cannot convert empty entity");
		}

		if (burnedCalories.getSportActivity() == null) {
			throw new IllegalArgumentException("Burned calories cannot have empty sportActivity");
		}

		final BurnedCaloriesTO burnedCaloriesTO = new BurnedCaloriesTO();

		final String sourceId = burnedCalories.getId();
		final String sourceCalories = burnedCalories.getCalories();
		final String sourceSpeed = burnedCalories.getSpeed();
		final String sourceWeight = burnedCalories.getWeight();

		burnedCaloriesTO.setId(sourceId == null ? null : Long.valueOf(sourceId));
		burnedCaloriesTO.setCalories(sourceCalories == null ? null : Long.valueOf(sourceCalories));
		burnedCaloriesTO.setSpeed(sourceSpeed == null ? null : Float.valueOf(sourceSpeed));
		burnedCaloriesTO.setWeight(sourceWeight == null ? null : Float.valueOf(sourceWeight));

		burnedCaloriesTO.setSportActivity(converter.convert2SportActivityTO(burnedCalories.getSportActivity()));
		return burnedCaloriesTO;
	}
}
